Micron Document
<!DOCTYPE html>
<html class="client-nojs vector-feature-night-mode-disabled vector-feature-language-in-header-enabled vector-feature-language-in-main-page-header-disabled vector-feature-page-tools-pinned-disabled vector-feature-toc-pinned-clientpref-1 vector-feature-main-menu-pinned-disabled vector-feature-limited-width-clientpref-1 vector-feature-limited-width-content-enabled vector-feature-custom-font-size-clientpref-1 vector-feature-appearance-pinned-clientpref-1 vector-sticky-header-enabled" lang="en" dir="ltr"><head>
<meta charset="UTF-8">
<title>Classpath</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="canonical" href="https://en.wikipedia.org/wiki/Classpath"> <link href="./mw/ext.cite.styles.css" rel="stylesheet" type="text/css">
<link href="./mw/ext.pygments.css" rel="stylesheet" type="text/css">
<link href="./mw/skins.vector.icons.css" rel="stylesheet" type="text/css">
<link href="./mw/skins.vector.search.codex.styles.css" rel="stylesheet" type="text/css">
<link href="./mw/skins.vector.styles.css" rel="stylesheet" type="text/css">
<link href="./mw/user.styles.css" rel="stylesheet" type="text/css">
<meta name="ResourceLoaderDynamicStyles" content="">
<link rel="stylesheet" type="text/css" href="./mw/site.styles.css">
<link rel="stylesheet" type="text/css" href="./mw/noscript.css">
<link rel="stylesheet" type="text/css" href="./footer.css">
<link rel="stylesheet" type="text/css" href="./vector-2022.css">
</head>
<body class="skin--responsive skin-vector skin-vector-search-vue mediawiki ltr sitedir-ltr mw-hide-empty-elt ns-0 ns-subject page-Classpath rootpage-Classpath skin-vector-2022 action-view">
<div class="mw-page-container">
<div class="mw-page-container-inner">
<div class="mw-content-container">
<main id="content" class="mw-body">
<header class="mw-body-header vector-page-titlebar">
<h1 id="firstHeading" class="firstHeading mw-first-heading">
<span id="openzim-page-title" class="mw-page-title-main"><span class="mw-page-title-main">Classpath</span></span>
</h1>
</header>
<a id="top"></a>
<div id="bodyContent" class="vector-body ve-init-mw-desktopArticleTarget-targetContainer" aria-labelledby="firstHeading" data-mw-ve-target-container="">
<div id="mw-content-text" class="mw-body-content mw-content-ltr" lang="en" dir="ltr"><div class="mw-content-ltr mw-parser-output" lang="en" dir="ltr"><style data-mw-deduplicate="TemplateStyles:r1236090951">
/* start https://en.wikipedia.org/ */


.mw-parser-output .hatnote{font-style:italic}.mw-parser-output div.hatnote{padding-left:1.6em;margin-bottom:0.5em}.mw-parser-output .hatnote i{font-style:normal}.mw-parser-output .hatnote+link+.hatnote{margin-top:-0.5em}@media print{body.ns-0 .mw-parser-output .hatnote{display:none!important}}


/* end https://en.wikipedia.org/ */
</style><div role="note" class="hatnote navigation-not-searchable">For the software, see <a href="GNU_Classpath" title="GNU Classpath">GNU Classpath</a>.</div>
<style data-mw-deduplicate="TemplateStyles:r1251242444">
/* start https://en.wikipedia.org/ */


.mw-parser-output .ambox{border:1px solid #a2a9b1;border-left:10px solid #36c;background-color:#fbfbfb;box-sizing:border-box}.mw-parser-output .ambox+link+.ambox,.mw-parser-output .ambox+link+style+.ambox,.mw-parser-output .ambox+link+link+.ambox,.mw-parser-output .ambox+.mw-empty-elt+link+.ambox,.mw-parser-output .ambox+.mw-empty-elt+link+style+.ambox,.mw-parser-output .ambox+.mw-empty-elt+link+link+.ambox{margin-top:-1px}html body.mediawiki .mw-parser-output .ambox.mbox-small-left{margin:4px 1em 4px 0;overflow:hidden;width:238px;border-collapse:collapse;font-size:88%;line-height:1.25em}.mw-parser-output .ambox-speedy{border-left:10px solid #b32424;background-color:#fee7e6}.mw-parser-output .ambox-delete{border-left:10px solid #b32424}.mw-parser-output .ambox-content{border-left:10px solid #f28500}.mw-parser-output .ambox-style{border-left:10px solid #fc3}.mw-parser-output .ambox-move{border-left:10px solid #9932cc}.mw-parser-output .ambox-protection{border-left:10px solid #a2a9b1}.mw-parser-output .ambox .mbox-text{border:none;padding:0.25em 0.5em;width:100%}.mw-parser-output .ambox .mbox-image{border:none;padding:2px 0 2px 0.5em;text-align:center}.mw-parser-output .ambox .mbox-imageright{border:none;padding:2px 0.5em 2px 0;text-align:center}.mw-parser-output .ambox .mbox-empty-cell{border:none;padding:0;width:1px}.mw-parser-output .ambox .mbox-image-div{width:52px}@media(min-width:720px){.mw-parser-output .ambox{margin:0 10%}}@media print{body.ns-0 .mw-parser-output .ambox{display:none!important}}


/* end https://en.wikipedia.org/ */
</style><style data-mw-deduplicate="TemplateStyles:r1248332772">
/* start https://en.wikipedia.org/ */


.mw-parser-output .multiple-issues-text{width:95%;margin:0.2em 0}.mw-parser-output .multiple-issues-text>.mw-collapsible-content{margin-top:0.3em}.mw-parser-output .compact-ambox .ambox{border:none;border-collapse:collapse;background-color:transparent;margin:0 0 0 1.6em!important;padding:0!important;width:auto;display:block}body.mediawiki .mw-parser-output .compact-ambox .ambox.mbox-small-left{font-size:100%;width:auto;margin:0}.mw-parser-output .compact-ambox .ambox .mbox-text{padding:0!important;margin:0!important}.mw-parser-output .compact-ambox .ambox .mbox-text-span{display:list-item;line-height:1.5em;list-style-type:disc}body.skin-minerva .mw-parser-output .multiple-issues-text>.mw-collapsible-toggle,.mw-parser-output .compact-ambox .ambox .mbox-image,.mw-parser-output .compact-ambox .ambox .mbox-imageright,.mw-parser-output .compact-ambox .ambox .mbox-empty-cell,.mw-parser-output .compact-ambox .hide-when-compact{display:none}


/* end https://en.wikipedia.org/ */
</style>
<p><b>Classpath</b> is a parameter in the <a href="Java_virtual_machine" title="Java virtual machine">Java Virtual Machine</a> or the <a href="Java_compiler" title="Java compiler">Java compiler</a> that specifies the location of user-defined <a href="Class_(computer_science)" class="mw-redirect" title="Class (computer science)">classes</a> and <a href="Java_package" title="Java package">packages</a>. The parameter may be set either on the <a href="Command_line_interface" class="mw-redirect" title="Command line interface">command-line</a>, or through an <a href="Environment_variable" title="Environment variable">environment variable</a>.
</p>
<meta property="mw:PageProp/toc">
<div class="mw-heading mw-heading2"><h2 id="Overview_and_architecture">Overview and architecture</h2></div>
<div role="note" class="hatnote navigation-not-searchable">See also: <a href="Java_class_loader" title="Java class loader">Java class loader</a></div>
<p>Similar to the classic <a href="Library_(computing)#Dynamic_loading" title="Library (computing)">dynamic loading</a> behavior, when executing <a href="Java_(programming_language)" title="Java (programming language)">Java</a> programs, the <a href="Java_virtual_machine" title="Java virtual machine">Java Virtual Machine</a> finds and loads classes lazily (it loads the <a href="Java_bytecode" title="Java bytecode">bytecode</a> of a class only when the class is first used). The classpath tells Java where to look in the filesystem for files defining these classes.
</p><p>The virtual machine searches for and loads classes in this order:
</p>
<ol><li>bootstrap classes: the classes that are fundamental to the <a href="Java_Platform" class="mw-redirect" title="Java Platform">Java Platform</a> (comprising the public classes of the <a href="Java_Class_Library" title="Java Class Library">Java Class Library</a>, and the private classes that are necessary for this library to be functional).</li>
<li>extension classes: <a href="Java_package" title="Java package">packages</a> that are in the <i>extension</i> directory of the <a href="Java_Runtime_Environment" class="mw-redirect" title="Java Runtime Environment">Java Runtime Environment</a> or <a href="JDK" class="mw-redirect" title="JDK">JDK</a>, <code>jre/lib/ext/</code></li>
<li>user-defined packages and libraries</li></ol>
<p>By default only the packages of the <a href="JDK" class="mw-redirect" title="JDK">JDK</a> <a href="Java_Platform%2C_Standard_Edition" title="Java Platform, Standard Edition">standard API</a> and extension packages are accessible without needing to set where to find them. The path for all user-defined <a href="Java_package" title="Java package">packages</a> and libraries must be set in the command-line (or in the <a href="Manifest_file" title="Manifest file">Manifest</a> associated with the <a href="JAR_(file_format)" title="JAR (file format)">Jar file</a> containing the classes).
</p>
<div class="mw-heading mw-heading2"><h2 id="Setting_the_path_to_execute_Java_programs">Setting the path to execute Java programs</h2></div>
<div class="mw-heading mw-heading3"><h3 id="Supplying_as_application_argument">Supplying as application argument</h3></div>
<p>Suppose we have a package called <i>org.mypackage</i> containing the classes:
</p>
<ul><li><i>HelloWorld</i> (main class)</li>
<li><i>SupportClass</i></li>
<li><i>UtilClass</i></li></ul>
<p>and the files defining this package are stored physically under the directory <i>D:\myprogram</i> (on <a href="Microsoft_Windows" title="Microsoft Windows">Windows</a>) or <i>/home/user/myprogram</i> (on <a href="Linux" title="Linux">Linux</a>).
</p><p>The file structure looks like this:
</p>
<table class="wikitable" style="font-size:90%">

<tbody><tr>
<th><a href="Microsoft_Windows" title="Microsoft Windows">Microsoft Windows</a>
</th>
<th><a href="Linux" title="Linux">Linux</a>
</th></tr>
<tr>
<td><pre>D:\myprogram\
|
---&gt; org\
|
---&gt; mypackage\
|
---&gt; HelloWorld.class
---&gt; SupportClass.class
---&gt; UtilClass.class
</pre>
</td>
<td><pre>/home/user/myprogram/
|
---&gt; org/
|
---&gt; mypackage/
|
---&gt; HelloWorld.class
---&gt; SupportClass.class
---&gt; UtilClass.class
</pre>
</td></tr>
</tbody></table>
<p>When we invoke Java, we specify the name of the application to run: org.mypackage.HelloWorld. However we must also tell Java where to look for the files and directories defining our package. So to launch the program, we use the following command:
</p>
<table class="wikitable" style="font-size:90%">

<tbody><tr>
<th><a href="Microsoft_Windows" title="Microsoft Windows">Microsoft Windows</a>
</th>
<th><a href="Linux" title="Linux">Linux</a>
</th></tr>
<tr>
<td><pre> java -classpath D:\myprogram org.mypackage.HelloWorld
</pre>
</td>
<td><pre> java -cp /home/user/myprogram org.mypackage.HelloWorld
</pre>
</td></tr>
</tbody></table>
<p>where:
</p>
<ul><li><code>java</code> is the <a href="Java_runtime" class="mw-redirect" title="Java runtime">Java runtime</a> launcher, a type of SDK Tool (A command-line tool, such as <a href="Javac" title="Javac">javac</a>, <a href="Javadoc" title="Javadoc">javadoc</a>, or <a href="APT_(software)" title="APT (software)">apt</a>)</li>
<li><i>-classpath D:\myprogram</i> sets the path to the packages used in the program (on Linux, <i>-cp /home/user/myprogram</i>) and</li>
<li><i>org.mypackage.HelloWorld</i> is the name of the main class</li></ul>
<div class="mw-heading mw-heading3"><h3 id="Setting_the_path_through_an_environment_variable">Setting the path through an environment variable</h3></div>
<p>The <a href="Environment_variable" title="Environment variable">environment variable</a> named <code>CLASSPATH</code> may be alternatively used to set the classpath. For the above example, we could also use on Windows:
</p>
<div class="mw-highlight mw-highlight-lang-doscon mw-content-ltr" dir="ltr"><pre><span class="gp">D:\myprogram&gt;</span><span class="k">set</span> <span class="nv">CLASSPATH</span><span class="p">=</span>D:\myprogram
<span class="gp">D:\myprogram&gt;</span>java org.mypackage.HelloWorld
</pre></div>
<p>The rule is that <code>-classpath</code> option, when used to start the java application, overrides the <code>CLASSPATH</code> environment variable. If none are specified, the <a href="Working_directory" title="Working directory">current working directory</a> is used as classpath. This means that when our working directory is <code>D:\myprogram\</code> (on Linux, <code>/home/user/myprogram/</code>), we would not need to specify the classpath explicitly. When overriding however, it is advised to include the current folder <code>"."</code> into the classpath in the case when loading classes from current folder is desired.
</p><p>The same applies not only to java launcher but also to <a href="Javac" title="Javac">javac</a>, the java compiler.
</p>
<div class="mw-heading mw-heading3"><h3 id="Setting_the_path_of_a_Jar_file">Setting the path of a Jar file</h3></div>
<p>If a program uses a supporting library enclosed in a <a href="JAR_(file_format)" title="JAR (file format)">Jar file</a> called <i>supportLib.jar</i>, physically located in the directory <i>D:\myprogram\lib\</i> and the corresponding physical file structure is:
</p>
<pre>D:\myprogram\
|
---&gt; lib\
|
---&gt; supportLib.jar
|
---&gt; org\
|
---&gt; mypackage\
|
---&gt; HelloWorld.class
---&gt; SupportClass.class
---&gt; UtilClass.class
</pre>
<p>the following <a href="Command-line_interface#Command-line_option" title="Command-line interface">command-line option</a> is needed:
</p>
<pre>java -classpath D:\myprogram;D:\myprogram\lib\supportLib.jar org.mypackage.HelloWorld
</pre>
<p>or alternatively:
</p>
<div class="mw-highlight mw-highlight-lang-doscon mw-content-ltr" dir="ltr"><pre><span class="gp">D:\myprogram&gt;</span><span class="k">set</span> <span class="nv">CLASSPATH</span><span class="p">=</span>D:\myprogram;D:\myprogram\lib\supportLib.jar
<span class="gp">D:\myprogram&gt;</span>java org.mypackage.HelloWorld
</pre></div>
<div class="mw-heading mw-heading3"><h3 id="Adding_all_JAR_files_in_a_directory">Adding all JAR files in a directory</h3></div>
<p>In <a href="Java_6" class="mw-redirect" title="Java 6">Java 6</a> and higher, one can add all jar-files in a specific directory to the classpath using wildcard notation.
</p><p>Windows example:
</p>
<div class="mw-highlight mw-highlight-lang-doscon mw-content-ltr" dir="ltr"><pre><span class="gp">D:\myprogram&gt;</span>java -classpath <span class="s2">".;c:\mylib\*"</span> MyApp
</pre></div>
<p>Linux example:
</p>
<div class="mw-highlight mw-highlight-lang-console mw-content-ltr" dir="ltr"><pre><span class="gp">$ </span>java<span class="w"> </span>-classpath<span class="w"> </span><span class="s1">'.:/mylib/*'</span><span class="w"> </span>MyApp
</pre></div>
<p>This works for both <code>-classpath</code> options and environment classpaths.
</p>
<div class="mw-heading mw-heading3"><h3 id="Setting_the_path_in_a_manifest_file">Setting the path in a manifest file</h3></div>
<p>If a program has been enclosed in a <a href="JAR_(file_format)" title="JAR (file format)">Jar file</a> called <i>helloWorld.jar</i>, located directly in the directory <i>D:\myprogram</i>, the directory structure is as follows:
</p>
<pre>D:\myprogram\
|
---&gt; helloWorld.jar
|
---&gt; lib\
|
---&gt; supportLib.jar
</pre>
<p>The <a href="Manifest_file" title="Manifest file">manifest file</a> defined in <i>helloWorld.jar</i> has this definition:
</p>
<div class="mw-highlight mw-highlight-lang-properties mw-content-ltr" dir="ltr"><pre><span class="na">Main-Class</span><span class="o">:</span><span class="w"> </span><span class="s">org.mypackage.HelloWorld</span>
<span class="na">Class-Path</span><span class="o">:</span><span class="w"> </span><span class="s">lib/supportLib.jar</span>
</pre></div>
<p>The <a href="Manifest_file" title="Manifest file">manifest file</a> should end with either a new line or carriage return.
</p><p>The program is launched with the following command:
</p>
<pre>java -jar D:\myprogram\helloWorld.jar [app arguments]
</pre>
<p>This automatically starts <i>org.mypackage.HelloWorld</i> specified in class <i>Main-Class</i> with the arguments. The user cannot replace this class name using the invocation <code class="mw-highlight mw-highlight-lang-text mw-content-ltr" style="" dir="ltr">java -jar</code>. <i>Class-Path</i> describes the location of <i>supportLib.jar</i> relative to the location of the library <i>helloWorld.jar</i>. Neither absolute file path, which is permitted in <code class="mw-highlight mw-highlight-lang-text mw-content-ltr" style="" dir="ltr">-classpath</code> parameter on the command line, nor jar-internal paths are supported. This means that if the main class file is contained in a jar, <i>org/mypackage/HelloWorld.class</i> must be a valid path on the root within the jar.
</p><p>Multiple classpath entries are separated with spaces:
</p>
<div class="mw-highlight mw-highlight-lang-properties mw-content-ltr" dir="ltr"><pre><span class="na">Class-Path</span><span class="o">:</span><span class="w"> </span><span class="s">lib/supportLib.jar lib/supportLib2.jar</span>
</pre></div>
<div class="mw-heading mw-heading2"><h2 id="OS_specific_notes">OS specific notes</h2></div>
<p>Being closely associated with the file system, the <a href="Command-line_argument" class="mw-redirect" title="Command-line argument">command-line</a> Classpath syntax depends on the operating system.<sup id="cite_ref-1" class="reference"><a href="#cite_note-1"><span class="cite-bracket">[</span>1<span class="cite-bracket">]</span></a></sup> For example:
</p>
<ul><li>on all <a href="Unix" title="Unix">Unix-like</a> operating systems (such as <a href="Linux" title="Linux">Linux</a> and <a href="Mac_OS_X" class="mw-redirect" title="Mac OS X">Mac OS X</a>), the directory structure has a Unix syntax, with separate file paths separated by a <a href="Colon_(punctuation)" title="Colon (punctuation)">colon</a> (":").</li>
<li>on <a href="Microsoft_Windows" title="Microsoft Windows">Windows</a>, the directory structure has a Windows syntax, and each file path must be separated by a <a href="Semicolon" title="Semicolon">semicolon</a> (";").</li></ul>
<p>This does not apply when the Classpath is defined in <a href="Manifest_file" title="Manifest file">manifest files</a>, where each file path must be separated by a space ("&nbsp;"), regardless of the operating system.
</p>
<div class="mw-heading mw-heading2"><h2 id="See_also">See also</h2></div>
<ul><li><a href="Java_class_loader" title="Java class loader">Java class loader</a></li>
<li><a href="Java_Platform_Module_System" title="Java Platform Module System">Java Platform Module System</a></li>
<li><a href="Rpath" title="Rpath">rpath</a>&nbsp;– Hard-coded search path</li></ul>
<div class="mw-heading mw-heading2"><h2 id="References">References</h2></div>
<style data-mw-deduplicate="TemplateStyles:r1239543626">
/* start https://en.wikipedia.org/ */


.mw-parser-output .reflist{margin-bottom:0.5em;list-style-type:decimal}@media screen{.mw-parser-output .reflist{font-size:90%}}.mw-parser-output .reflist .references{font-size:100%;margin-bottom:0;list-style-type:inherit}.mw-parser-output .reflist-columns-2{column-width:30em}.mw-parser-output .reflist-columns-3{column-width:25em}.mw-parser-output .reflist-columns{margin-top:0.3em}.mw-parser-output .reflist-columns ol{margin-top:0}.mw-parser-output .reflist-columns li{page-break-inside:avoid;break-inside:avoid-column}.mw-parser-output .reflist-upper-alpha{list-style-type:upper-alpha}.mw-parser-output .reflist-upper-roman{list-style-type:upper-roman}.mw-parser-output .reflist-lower-alpha{list-style-type:lower-alpha}.mw-parser-output .reflist-lower-greek{list-style-type:lower-greek}.mw-parser-output .reflist-lower-roman{list-style-type:lower-roman}


/* end https://en.wikipedia.org/ */
</style><div class="reflist">
<div class="mw-references-wrap"><ol class="references">
<li id="cite_note-1"><span class="mw-cite-backlink"><b><a href="#cite_ref-1">^</a></b></span> <span class="reference-text"><style data-mw-deduplicate="TemplateStyles:r1238218222">
/* start https://en.wikipedia.org/ */


.mw-parser-output cite.citation{font-style:inherit;word-wrap:break-word}.mw-parser-output .citation q{quotes:"\"""\"""'""'"}.mw-parser-output .citation:target{background-color:rgba(0,127,255,0.133)}.mw-parser-output .id-lock-free.id-lock-free a{background:url("./mw/Lock-green.svg")right 0.1em center/9px no-repeat}.mw-parser-output .id-lock-limited.id-lock-limited a,.mw-parser-output .id-lock-registration.id-lock-registration a{background:url("./mw/Lock-gray-alt-2.svg")right 0.1em center/9px no-repeat}.mw-parser-output .id-lock-subscription.id-lock-subscription a{background:url("./mw/Lock-red-alt-2.svg")right 0.1em center/9px no-repeat}.mw-parser-output .cs1-ws-icon a{background:url("./mw/Wikisource-logo.svg")right 0.1em center/12px no-repeat}body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-free a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-limited a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-registration a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-subscription a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .cs1-ws-icon a{background-size:contain;padding:0 1em 0 0}.mw-parser-output .cs1-code{color:inherit;background:inherit;border:none;padding:inherit}.mw-parser-output .cs1-hidden-error{display:none;color:var(--color-error,#d33)}.mw-parser-output .cs1-visible-error{color:var(--color-error,#d33)}.mw-parser-output .cs1-maint{display:none;color:#085;margin-left:0.3em}.mw-parser-output .cs1-kern-left{padding-left:0.2em}.mw-parser-output .cs1-kern-right{padding-right:0.2em}.mw-parser-output .citation .mw-selflink{font-weight:inherit}@media screen{.mw-parser-output .cs1-format{font-size:95%}html.skin-theme-clientpref-night .mw-parser-output .cs1-maint{color:#18911f}}@media screen and (prefers-color-scheme:dark){html.skin-theme-clientpref-os .mw-parser-output .cs1-maint{color:#18911f}}


/* end https://en.wikipedia.org/ */
</style><cite class="citation web cs1"><a rel="nofollow" class="external text" href="http://www.antwerkz.com/blog/the-classpath">"The Classpath"</a>. 6 August 2013<span class="reference-accessdate">. Retrieved <span class="nowrap">2016-06-26</span></span>.</cite></span>
</li>
</ol></div></div>
<div class="mw-heading mw-heading2"><h2 id="External_links">External links</h2></div>
<ul><li><a rel="nofollow" class="external text" href="http://docs.oracle.com/javase/6/docs/technotes/tools/findingclasses.html">Note explaining how Java classes are found, on Oracle website</a></li>
<li><a rel="nofollow" class="external text" href="http://docs.oracle.com/javase/1.5.0/docs/tooldocs/windows/classpath.html">Specification of how to set the Classpath on Oracle site</a></li></ul></div><!--htdig_noindex--><div><div class="zim-footer">
This article is issued from <a class="external text" title="Last edited on 2025-01-26" href="https://en.wikipedia.org/wiki/?title=Classpath&amp;oldid=1271981878">Wikipedia</a>. The text is available under <a class="external text" href="https://creativecommons.org/licenses/by-sa/4.0/deed.en">Creative Commons Attribution-Share Alike 4.0</a> unless otherwise noted. Additional terms may apply for the media files.
</div>
</div><!--/htdig_noindex--></div>
</div>
</main>
</div>
</div>
</div>

</body></html>